//--------------------Sonic CD UFO Script---------------------//
//--------Scripted by Christian Whitehead 'The Taxman'--------//
//-------Unpacked By Rubberduckycooly's Script Unpacker-------//

// Aliases
private alias Object.YPos : Object.ZPos
private alias Object.Value0 : Object.TargetNode
private alias Object.Value1 : Object.XVelocity
private alias Object.Value2 : Object.ZVelocity
private alias Object.Value3 : Object.Timer
private alias Object.Value4 : Object.ScreenXPos // Difference from the center of the camera (which is where Sonic is always located)
private alias Object.Value5 : Object.ScreenDepth
private alias Object.Value6 : Object.PositionZ

private alias Object.Value8 : Object.DoesExist



// SFX Aliases
private alias 1 : SFX_G_RING

// Property Value
private alias 0 : UFO_CONTENTS_RINGS
private alias 1 : UFO_CONTENTS_SPEEDSHOES
private alias 2 : UFO_CONTENTS_TIME

// Priority
private alias 1 : PRIORITY_ACTIVE


event ObjectMain
	
	// Check player interaction in this (rather lengthy) process
	
	// X difference
	temp0 = Object.XPos
	temp0 -= TileLayer[0].XPos
	temp0 >>= 8
	
	// Z mutliplier
	temp1 = Object.ZPos
	temp1 -= TileLayer[0].ZPos
	temp1 >>= 8
	
	// Calculating the X difference
	Sin(temp2, TileLayer[0].Angle)
	temp2 *= temp1
	Cos(temp3, TileLayer[0].Angle)
	temp3 *= temp0
	Object.ScreenXPos = temp2
	Object.ScreenXPos -= temp3
	Object.ScreenXPos >>= 9
	
	// Calculating the Z difference
	Cos(temp2, TileLayer[0].Angle)
	temp2 *= temp1
	Sin(temp3, TileLayer[0].Angle)
	temp3 *= temp0
	Object.ScreenDepth = temp2
	Object.ScreenDepth += temp3
	Object.ScreenDepth >>= 9

switch object.state
case 0

	foreach (TypeName[Sonic], arrayPos2, ALL_ENTITIES)	

		// The UFO has to be within the 2100 range from the player, who's at a depth of 0x5800 normally
		if Object.ScreenDepth < 0x18C8
			if Object.ScreenDepth > 0x1094
				
				// The UFO has to be between -0.125 and +0.125 pixels away from the screen centre
				if Object.ScreenXPos > -0x2000
					if Object.ScreenXPos < 0x2000
						if options.gamemode == 0
							Object[4].Value0--
						else
							Object[4].Value0++
							FlashBattery += 200
						end if
						playsfx(SfxName[Page Collect], 0)
						if options.gamemode == 0
							object.type = TypeName[Blank Object]
						else
							object.visible = 0
							object.state++
						end if	
					end if
				end if
			end if
		end if
	next
break
case 1
	object.timer++
	CheckEqual(object.timer, 2000)
	temp0 = checkResult
	CheckEqual(Object[4].Value0, 1)
	temp0 |= checkResult	
	if temp0 == 1
		object.timer = 0
		playsfx(SfxName[respawn], 0)
		object.visible = 1
		object.state = 0
	end if
break
end switch

if object[6].state == 1
	object.type = TypeName[Blank Object]
end if

end event


event ObjectDraw

CheckEqual(inputdown[2].buttonY,1)
temp3 = checkResult
CheckEqual(ThunderActive,1)
temp3 |= checkResult
if temp3 == 1
	if Object.ScreenDepth > 256 // Don't draw if the UFO is too close to the screen
		
		// Do some maths to find the position & scale the UFO should be at
		
		Object.Scale = 0x2000000
		Object.Scale /= Object.ScreenDepth
		
		temp0 = 768
		temp0 -= Object.Scale
		temp0 /= 6
		
		temp1 = Object.ScreenDepth
		temp1 >>= 12
		
		if temp0 > 0
			Object.Scale += temp0
			Object.Scale -= temp1
		end if
		
		temp0 = Object.ScreenXPos
		temp0 *= 192
		temp0 /= Object.ScreenDepth
		temp0 += Screen.xcenter
		
		temp1 = TileLayer[0].YPos
		temp1 >>= 8
		temp1 -= 0x2000
		temp1 *= 96
		temp1 /= Object.ScreenDepth
		temp1 += 128
		
		// Draw the UFO now
		
		
		// Find the ground position
		temp1 = TileLayer[0].YPos
		temp1 >>= 8
		temp1 *= 96
		temp1 /= Object.ScreenDepth
		temp1 += 128
		

		if object.visible == 1

		DrawSpriteScreenFX(3, FX_SCALE, temp0, temp1)
		

		DrawSpriteScreenFX(1, FX_SCALE, temp0, temp1)

		end if
		
	end if
end if	
end event


event ObjectStartup

	LoadSpriteSheet("3D_Test/Objects.gif")
	
	// UFO Frames
	
	// 0 - 10 Rings Variant
	SpriteFrame(-40, -110, 80, 112, 258, 1)
	
	// 1 - Speed Shoes Variant
	SpriteFrame(-15	, -28, 32, 32, 257, 181)
	
	// 2 - Extra Time Variant
	SpriteFrame(-40, -28, 80, 56, 163, 1)
	
	// 3 - UFO Shadow Frame
	SpriteFrame(-7, -8, 18, 8, 83, 117)
	
	// Cycle through all UFO objects in the scene and set them up
	ArrayPos0 = 32
	while ArrayPos0 < 1056
		if Object[ArrayPos0].Type == TypeName[Pages]
			
			// Set the UFO to always be active
			Object[ArrayPos0].Priority = PRIORITY_ACTIVE
			

			if options.gamemode == 0
				Object[4].Value0++
			else
				lamppostID = 0
				Object[4].Value0 = 1
			end if
		
		end if
		
		ArrayPos0++
	loop
	
end event


// ========================
// Editor events
// ========================

event RSDKEdit
	if Editor.ReturnVariable == true
		switch Editor.VariableID
		case EDIT_VAR_PROPVAL // Property Value
			CheckResult = Object.PropertyValue
			break
		case 0 // Contents
			CheckResult = Object.PropertyValue
			break
		end switch
	else
		switch Editor.VariableID
		case EDIT_VAR_PROPVAL // Property Value
			Object.PropertyValue = Editor.VariableValue
			break
		case 0 // Contents
			Object.PropertyValue = Editor.VariableValue
			break
		end switch
	end if
end event


event RSDKDraw
	DrawSprite(Object.PropertyValue)
	
	if Editor.ShowGizmos == true
		// Draw arrows to show the network of UFO nodes
		
		Editor.DrawingOverlay = true
		
		ArrayPos1 = Object.entityPos
		ArrayPos0 = Object.entityPos
		ArrayPos0++
		
		// TODO: change this to "UFO Node" once GetObjectType works with spaces
		GetObjectType(temp0, "UFONode")
		
		while Object[ArrayPos0].Type == temp0
			DrawArrow(Object[ArrayPos1].XPos, Object[ArrayPos1].ZPos, Object[ArrayPos0].XPos, Object[ArrayPos0].ZPos, 255, 255, 0)
			ArrayPos0++
			ArrayPos1++
		loop
		
		ArrayPos0 = Object.entityPos
		ArrayPos0++
		if ArrayPos1 != Object.entityPos
			DrawArrow(Object[ArrayPos1].XPos, Object[ArrayPos1].ZPos, Object[ArrayPos0].XPos, Object[ArrayPos0].ZPos, 255, 255, 0)
		end if
		
		Editor.DrawingOverlay = false
	end if
end event


event RSDKLoad
	LoadSpriteSheet("3D_Test/Objects.gif")
	SpriteFrame(-40, -28, 80, 56, 1, 1)
	SpriteFrame(-40, -28, 80, 56, 82, 1)
	SpriteFrame(-40, -28, 80, 56, 163, 1)

	AddEditorVariable("Contents")
	SetActiveVariable("Contents")
	AddEnumVariable("10 Rings", UFO_CONTENTS_RINGS)
	AddEnumVariable("Speed Shoes", UFO_CONTENTS_SPEEDSHOES)
	AddEnumVariable("Extra Time", UFO_CONTENTS_TIME)
end event
